from odoo import api, fields, models


class Sale(models.Model):
    _name = 'dragon.sale'
    _description = '三龙百货销售单据'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _rec_name = 'name'

    @api.one
    @api.depends('goods_ids.sum_money')
    def compute_total(self):
        for line in self.goods_ids:
            print(self.id)
            self.total += line.sum_money

    @api.one
    @api.depends('total', 'cut_quota')
    def compute_real(self):
        self.ensure_one()
        for line in self:
            line.real_amount = line.total - line.cut_quota

    name = fields.Char(
        '标题',
        default='三龙百货销售单详情'
    )

    bill_no = fields.Char(
        string='单据编号',
        index=True,
        readonly=True,
        default='自动生成'
    )

    @api.model
    def create(self, vals):
        """
        自动生成单号
        :return:
        """
        vals['bill_no'] = self.env['ir.sequence'].next_by_code('dragon.sale') or ''
        return super(Sale, self).create(vals)

    input_date = fields.Date(
        '录单日期',
        default=lambda self: fields.Date.today(),
    )

    store = fields.Many2one(
        'dragon.store',
        string='白联仓库',
        required=True,
        ondelete='set null'
    )

    store_addr = fields.Char(
        string='仓库地址',
        related='store.addr',
        readonly=True
    )

    store_phone = fields.Char(
        string='订货电话',
        related='store.phone',
        readonly=True
    )

    custom = fields.Many2one(
        'dragon.custom',
        string='客户名称',
        required=True,
        ondelete='set null'
    )

    custom_addr = fields.Char(
        string='客户地址',
        related='custom.addr',
        readonly=True
    )

    custom_phone = fields.Char(
        string='联系电话',
        related='custom.phone',
        readonly=True
    )

    custom_ali = fields.Char(
        '支付宝账号',
        related='custom.ali',
        readonly=True
    )

    custom_abc = fields.Char(
        '已转农行',
        related='custom.abc',
        readonly=True
    )

    custom_cash = fields.Char(
        '收现金',
        related='custom.cash',
        readonly=True
    )

    goods_ids = fields.One2many(
        'dragon.goods.line',
        'sale_id',
        string='商品清单',
        required=True,
        ondelete='set null'
    )

    handler = fields.Many2one(
        'res.users',
        string='经手人',
        required=True,
        ondelete='set null'
    )

    ought_get = fields.Float(
        '此前应收'
    )

    cut_quota = fields.Float(
        '优惠金额'
    )
    total = fields.Float(
        '总计金额',
        compute='compute_total',
        store=True
    )
    real_amount = fields.Float(
        '优惠后金额',
        readonly=True,
        compute='compute_real',
        store=True
    )


class GoodsLine(models.Model):
    _name = 'dragon.goods.line'
    _description = '三龙百货商品单据体'
    _rec_name = 'name'

    @api.one
    @api.depends('amount', 'price')
    def _compute_amount_total(self):
        for line in self:
            line.sum_money = line.amount * line.price

    sale_id = fields.Many2one(
        'dragon.sale'
    )

    goods_id = fields.Many2one(
        'dragon.goods',
        required=True,
        string='商品全名'
    )

    sequence = fields.Integer(
        string='Sequence'
    )

    barcode = fields.Char(
        '商品条码',
        related='goods_id.barcode',
    )

    good_number = fields.Char(
        '商品编号',
        related='goods_id.good_number',
    )

    name = fields.Char(
        '商品全名',
        related='goods_id.name',
    )

    package_type = fields.Char(
        '包装规格',
        related='goods_id.package_type',
    )

    unit = fields.Char(
        '单位',
        related='goods_id.unit',
    )

    amount = fields.Integer(
        '数量'
    )

    price = fields.Float(
        '单价',
        related='goods_id.price',
    )

    sum_money = fields.Float(
        '金额',
        readonly=True,
        compute='_compute_amount_total'
    )

    remark = fields.Char(
        '备注'
    )
